package com.zyd.shiro.business.controller.pak;

import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Repair;
import com.zyd.shiro.business.entity.Scrapped;
import com.zyd.shiro.business.service.PakAssetsService;
import com.zyd.shiro.business.service.PakScrappedService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.persistence.beans.PakRepair;
import com.zyd.shiro.util.ResultUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.zyd.shiro.persistence.beans.PakScrapped;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.io.File;
import com.zyd.shiro.utils.FileUtils;
import com.zyd.shiro.utils.ExcelUtil;
import java.util.Date;


@RestController
@RequestMapping("/scrapped")
@Api(description = "资产报废")
public class PakScrappedController {
    @Autowired
    private PakScrappedService scrappedService;

    @Autowired
    private PakAssetsService assetsService;

    @PostMapping
@ApiOperation(value="添加", produces="application/json")
    public ResponseVO insert(Scrapped scrapped) {
        scrappedService.insert(scrapped);
        return ResultUtil.success("添加成功！",scrapped);
    }

    @PutMapping
    @ApiOperation(value="修改", produces="application/json")
    public ResponseVO update(Scrapped scrapped) {
        scrappedService.updateSelective(scrapped);
        return ResultUtil.success("修改成功！",scrapped);
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value="主键删除", produces="application/json")
    public ResponseVO delete(@PathVariable(name="id") Long id){
        scrappedService.removeByPrimaryKey(id);
        return ResultUtil.success("删除成功！",null);
    }

    @GetMapping
    @ApiOperation(value="分页查询", produces="application/json")
    public PageResult page(BaseConditionVO vo) {
        PageInfo<Scrapped> pageInfo = scrappedService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }

    @GetMapping(value = "/all")
    @ApiOperation(value="查询所有", produces="application/json")
    public ResponseVO list() {
        List<Scrapped> scrappeds = scrappedService.listAll();
        return ResultUtil.success("查询成功！",scrappeds);
    }

   /* @PostMapping(value = "/import")
    @ApiOperation(value="批量导入", produces="application/json")
    public ResponseVO folderImport(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.indexOf("."), fileName.length());
        if (suffix.equals(".xlsx") || suffix.equals(".xls")) {
            File newFile = new File(FileUtils.createFilePath(fileName));
            file.transferTo(newFile);
            ExcelUtil<PakScrapped> excelUtil = new ExcelUtil<>();
            List<PakScrapped> acfForms = excelUtil.importExcel("Sheet1", newFile, PakScrapped.class);
            for (PakScrapped a : acfForms) {
                a.setCreateTime(new Date());
                a.setUpdateTime(new Date());
                scrappedService.increase(a);
            }
            return ResultUtil.success("修改成功！");
        }
        return ResultUtil.error("请输入excel文件!");
    }*/
    @PutMapping(value = "/approve/{id}")
    @ApiOperation(value="审批", produces="application/json")
    public ResponseVO approve(@PathVariable(name="id") Long id,int status) {
        Scrapped scrapped = scrappedService.getByPrimaryKey(id);
        scrapped.getPakScrapped().setAppstatus(status);
        scrappedService.updateSelective(scrapped);
        return ResultUtil.success("审批成功！",scrapped);
    }

    @PutMapping(value = "/scrapped/{id}")
    @ApiOperation(value="报废", produces="application/json")
    public ResponseVO scrapped(@PathVariable(name="id") Long id) {
        Scrapped scrapped = scrappedService.getByPrimaryKey(id);
        PakScrapped pakScrapped = scrapped.getPakScrapped();
        if(pakScrapped.getAppstatus()!=1){
            return ResultUtil.error("未通过审批！");
        }
        if(pakScrapped.getScrstatus()!=0){
            return ResultUtil.error("正在处理中！");
        }
        pakScrapped.setScrstatus(1);
        assetsService.scrapped(pakScrapped);
        scrappedService.updateSelective(scrapped);
        return ResultUtil.success("报废成功！",scrapped);

    }
}